import { getChannelList, getAllChannelList } from '@/api/channel'
const state = {
  channels: [],
  allChannels: []
}
const mutations = {
  setChannels(state, payload) {
    state.channels = payload
  },
  setAllChannels(state, allChannels) {
    state.allChannels = allChannels
  },
  addChannel(state, payload) {
    state.channels.push(payload)
  },
  delChannel(state, payload) {
    // filter map every some forEach reduce
    state.channels = state.channels.filter(item => item.id !== payload.id)
  }
}
const actions = {
  async getChannels({ commit }) {
    const res = await getChannelList()
    commit('setChannels', res.data.data.channels)
  },
  async getAllChannels({ commit }) {
    const res = await getAllChannelList()
    commit('setAllChannels', res.data.data.channels)
  }
}
// 相当于组件的计算属性
const getters = {
  // 可选的频道
  optionalChannels(state) {
    return state.allChannels.filter(item => {
      const index = state.channels.findIndex(v => v.id === item.id)
      return index === -1
    })
  }
}

export default {
  // 开启命名空间
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
